package persistency.reader;

/**
 * CourseReader.java
 * Purpose: this class represents a course that students can take
 * 
 * @author Hans Brems
 * @version 1.0 05/11/2011
 */

import java.util.ArrayList;

import model.OfferedCourse;
import model.Registration;

import model.container.CourseContainer;
import model.container.RegistrationContainer;

public class RegistrationOfferedCourseReader extends ContentReader {
	
	/** Constructs a course reader object **/
	public RegistrationOfferedCourseReader() {
	}
	
	public RegistrationOfferedCourseReader(String filePath) {
		super(filePath);
	}
	
	/** Reads a text file containing course objects into a collection */
	public ArrayList<Registration> read() {
		
		// Create list
		ArrayList<Registration> registrations= new ArrayList<Registration>();
		
		// Skip the first line (header)
		this.scanner.nextLine();

		// Construct a teacher object from each line
		while (this.scanner.hasNext()) {
			
			// Create a 
			int ocKey = Integer.parseInt(this.scanner.next());
			OfferedCourse offeredCourse= CourseContainer.getInstance().getOfferedCourseById(ocKey);
			int regKey = Integer.parseInt(this.scanner.next());
			Registration registration = RegistrationContainer.getInstance().getRegistrationByID(regKey);
			
			if(registration != null && offeredCourse != null) {
				registration.setOfferedCourse(offeredCourse);
				offeredCourse.getRegistrations().add(registration);
				registrations.add(registration);
			}
		}
		
		// Return list
		return registrations;
	}
}